package com.md.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.md.dao.UserDao;
import com.md.entity.DeptInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDaoImpl implements UserDao {
    @Override
    public int deleteUserById(int id) {
        return id>0&&id<=3?1:0;
    }

    @Override
    public DeptInfo selectUserById(int id) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource bean = context.getBean(DruidDataSource.class);
        try {
            DruidPooledConnection connection = bean.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from DeptInfo where did = #{id}");
            while (resultSet.next()){
                return new DeptInfo(resultSet.getInt("did"),resultSet.getString("dName"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return new DeptInfo();
    }
}
